#include "plotpopupmenu.h"
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QWidgetAction>
#include "curve.h"

PlotPopupMenu::PlotPopupMenu(QWidget *parent)
    : QMenu (parent)
{
    m_menuFullScreen = new QMenu(tr("FullScreen"), this);
    addFullScreenActions();

    m_menuVisible = new QMenu(tr("Visible"), this);
    addFullScreenActions();

    m_actClear = new QAction(tr("Clear"), this);
    m_actRange = new QAction(tr("Range"), this);
    m_actUpdateTime = new QAction(tr("UpdateTime"), this);

    m_actScopeCurveUpdate = new QAction(tr("ScopeCurveUpdate"), this);
    m_actScopeCurveUpdate->setCheckable(true);
    m_actScopeFollow = new QAction(tr("ScopeFollow"), this);
    m_actScopeFollow->setCheckable(true);
    m_actPeriodUpdate = new QAction(tr("PeriodUpdate"), this);
    m_actPeriodUpdate->setCheckable(true);

    m_actPause = new QAction(tr("Pause"), this);
    m_actPause->setCheckable(true);

    m_actNoise = new QAction(tr("Noise"), this);
    m_actShift = new QAction(tr("Shift"), this);

    m_actSave = new QAction(tr("Save"), this);
    m_actSaveAsPng = new QAction(tr("SaveAsPng"), this);
    m_actImport = new QAction(tr("Import"), this);

    m_actSavePeriod = new QAction(tr("SavePeriod"), this);
//    m_actSavePeriod->setCheckable(true);

    m_actFFT = new QAction(tr("FFT"), this);

    addSeparator();

    addMenu(m_menuFullScreen);
    addMenu(m_menuVisible);
    addAction(m_actClear);
    addSeparator();
    addAction(m_actRange);
    addAction(m_actUpdateTime);
    addSeparator();
    addAction(m_actScopeFollow);
    addAction(m_actScopeCurveUpdate);
    addAction(m_actPeriodUpdate);
    addSeparator();
    addAction(m_actPause);
    addSeparator();
    addAction(m_actNoise);
    addAction(m_actShift);
    addSeparator();
    addAction(m_actSave);
    addAction(m_actSaveAsPng);
    addAction(m_actImport);
    addSeparator();
    addAction(m_actSavePeriod);
    addSeparator();
    addAction(m_actFFT);

#if 0
    QMenu *rangeMenu = new QMenu("Range", this);
    {
        {
            QWidget *w = new QWidget();
            QLabel *lb = new QLabel();
            lb->setText(tr("XMin"));
            QLineEdit *le = new QLineEdit();
            QHBoxLayout *hbl = new QHBoxLayout();
            hbl->addWidget(lb);
            hbl->addWidget(le);
            w->setLayout(hbl);
            QWidgetAction *wa = new QWidgetAction(this);
            wa->setDefaultWidget(w);
            rangeMenu->addAction(wa);
        }

        {
            QWidget *w = new QWidget();
            QLabel *lb = new QLabel();
            lb->setText(tr("XMax"));
            QLineEdit *le = new QLineEdit();
            QHBoxLayout *hbl = new QHBoxLayout();
            hbl->addWidget(lb);
            hbl->addWidget(le);
            w->setLayout(hbl);
            QWidgetAction *wa = new QWidgetAction(this);
            wa->setDefaultWidget(w);
            rangeMenu->addAction(wa);
        }

        {
            QWidget *w = new QWidget();
            QLabel *lb = new QLabel();
            lb->setText(tr("YMin"));
            QLineEdit *le = new QLineEdit();
            QHBoxLayout *hbl = new QHBoxLayout();
            hbl->addWidget(lb);
            hbl->addWidget(le);
            w->setLayout(hbl);
            QWidgetAction *wa = new QWidgetAction(this);
            wa->setDefaultWidget(w);
            rangeMenu->addAction(wa);
        }

        {
            QWidget *w = new QWidget();
            QLabel *lb = new QLabel();
            lb->setText(tr("YMax"));
            QLineEdit *le = new QLineEdit();
            QHBoxLayout *hbl = new QHBoxLayout();
            hbl->addWidget(lb);
            hbl->addWidget(le);
            w->setLayout(hbl);
            QWidgetAction *wa = new QWidgetAction(this);
            wa->setDefaultWidget(w);
            rangeMenu->addAction(wa);
        }

        {
            QWidget *w = new QWidget();
            QPushButton *pbEnter = new QPushButton();
            pbEnter->setText(tr("enter"));
            QHBoxLayout *hbl = new QHBoxLayout();
            hbl->addWidget(pbEnter);
            w->setLayout(hbl);
            QWidgetAction *wa = new QWidgetAction(this);
            wa->setDefaultWidget(w);
            rangeMenu->addAction(wa);
        }
    }
    addMenu(rangeMenu);
#endif

    connect(m_actClear, &QAction::triggered, [=](bool checked){
        Q_UNUSED(checked)
        emit selected(MT_Clear);
    });
    connect(m_actRange, &QAction::triggered, [=](bool checked){
        Q_UNUSED(checked)
        emit selected(MT_Range);
    });
    connect(m_actUpdateTime, &QAction::triggered, [=](bool checked){
        Q_UNUSED(checked)
        emit selected(MT_UpdateTime);
    });

    connect(m_actScopeFollow, &QAction::triggered, [=](bool checked){
        emit selected(MT_ScopeFollow, checked);
    });
    connect(m_actScopeCurveUpdate, &QAction::triggered, [=](bool checked){
        emit selected(MT_ScopeCurveUpdate, checked);
    });
    connect(m_actPeriodUpdate, &QAction::triggered, [=](bool checked){
        emit selected(MT_PeriodUpdate, checked);
    });
    connect(m_actPause, &QAction::triggered, [=](bool checked){
        emit selected(MT_Pause, checked);
    });
    connect(m_actNoise, &QAction::triggered, [=](bool checked){
        Q_UNUSED(checked)
        emit selected(MT_Noise);
    });
    connect(m_actShift, &QAction::triggered, [=](bool checked){
        Q_UNUSED(checked)
        emit selected(MT_Shift);
    });
    connect(m_actSave, &QAction::triggered, [=](bool checked){
        Q_UNUSED(checked)
        emit selected(MT_Save);
    });
    connect(m_actSaveAsPng, &QAction::triggered, [=](bool checked){
        Q_UNUSED(checked)
        emit selected(MT_SaveAsPng);
    });
    connect(m_actImport, &QAction::triggered, [=](bool checked){
        Q_UNUSED(checked)
        emit selected(MT_Import);
    });
    connect(m_actSavePeriod, &QAction::triggered, [=](bool checked){
        Q_UNUSED(checked)
        emit selected(MT_SavePeriod);
    });
    connect(m_actFFT, &QAction::triggered, [=](bool checked){
        Q_UNUSED(checked)
        emit selected(MT_FFT);
    });
}

PlotPopupMenu::~PlotPopupMenu()
{
    delete m_menuFullScreen;
    delete m_menuVisible;
    delete m_actClear;
    delete m_actRange;
    delete m_actUpdateTime;
    delete m_actScopeCurveUpdate;
    delete m_actScopeFollow;
    delete m_actPeriodUpdate;
    delete m_actPause;
    delete m_actNoise;
    delete m_actShift;
    delete m_actSave;
    delete m_actImport;
    delete m_actFFT;
}

void PlotPopupMenu::setConfig(bool isScopeCurveUpdate, bool isScopeFollow, bool isPeriodUpdate, bool isPause)
{
    m_actScopeCurveUpdate->setChecked(isScopeCurveUpdate);
    m_actScopeFollow->setChecked(isScopeFollow);
    m_actPeriodUpdate->setChecked(isPeriodUpdate);
    m_actPause->setChecked(isPause);
}

void PlotPopupMenu::setGraphCnt(int cnt)
{
    if (m_graphCnt != cnt)
    {
        m_graphCnt = cnt;

        m_menuFullScreen->clear();
        addFullScreenActions();

        m_menuVisible->clear();
        addVisibleActions();
    }
}

void PlotPopupMenu::setCurve(Curve &curve)
{
    m_curve = &curve;
}

void PlotPopupMenu::setVisibleMenu(QList<bool> visibles)
{
    if (visibles.size() != m_menuVisible->actions().size())
        return;

    for (int i = 0; i < visibles.size(); i++)
    {
        m_menuVisible->actions().at(i)->setChecked(visibles.at(i));
    }
}

void PlotPopupMenu::addFullScreenActions()
{
    QAction *act;
    for (int i = 0; i < m_graphCnt; i++)
    {
        QString name = m_curve->getGraphName(i);
        act = new QAction(name);

        connect(act, &QAction::triggered, [=](bool checked){
            Q_UNUSED(checked)
            emit fullScreen(i);
        });
        m_menuFullScreen->addAction(act);
    }

    act = new QAction(tr("All"));

    connect(act, &QAction::triggered, [=](bool checked){
        Q_UNUSED(checked)
        emit fullScreen(m_graphCnt);
    });
    m_menuFullScreen->addAction(act);
}

void PlotPopupMenu::addVisibleActions()
{
    QAction *act;
    for (int i = 0; i < m_graphCnt; i++)
    {
        QString name = m_curve->getGraphName(i);
        act = new QAction(name);
        act->setCheckable(true);

        connect(act, &QAction::triggered, [=](bool checked){
            emit setVisi(i, checked);
        });
        m_menuVisible->addAction(act);
    }
}
